package com.spring.elasticsearch.controller;

import com.spring.elasticsearch.entity.User;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaojf
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    @GetMapping("getUser")
    public Object getUser() {
        Map<String, Object> map = new HashMap<>(5);
        //条件
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        //本地连接
        NativeSearchQueryBuilder nativeQueryBuilder = new NativeSearchQueryBuilder();
        nativeQueryBuilder.withQuery(queryBuilder);
        NativeSearchQuery build = nativeQueryBuilder.build();

        SearchHits<User> userSearchHits = elasticsearchRestTemplate.search(build, User.class);

        SearchHits<User> userInfoSearchHits = elasticsearchRestTemplate.search(build, User.class, IndexCoordinates.of("user_info"));

        SearchHits<User> userInfoAddSearchHits = elasticsearchRestTemplate.search(build, User.class, IndexCoordinates.of("user_info_add"));
        SearchHit<User> searchHits = userSearchHits.getSearchHits().get(0);
        map.put("user", searchHits.getContent());

        SearchHit<User> userSearchHit = userInfoSearchHits.getSearchHits().get(0);
        map.put("user_info", userSearchHit.getContent());


        SearchHit<User> userInfoAddSearchHit = userInfoAddSearchHits.getSearchHits().get(0);
        map.put("user_info_add", userInfoAddSearchHit.getContent());

        return map;
    }

    /**
     * 获取indexName下的所有集合对象
     * @return
     */
    @GetMapping("search")
    public Object search() {
        Map<String, Object> map = new HashMap<>(5);
        //条件
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        //本地连接
        NativeSearchQueryBuilder nativeQueryBuilder = new NativeSearchQueryBuilder();
        nativeQueryBuilder.withQuery(queryBuilder);
        NativeSearchQuery build = nativeQueryBuilder.build();

        SearchHits<User> userSearchHits = elasticsearchRestTemplate.search(build, User.class);

        SearchHits<User> userInfoSearchHits = elasticsearchRestTemplate.search(build, User.class, IndexCoordinates.of("user_info"));

        SearchHits<User> userInfoAddSearchHits = elasticsearchRestTemplate.search(build, User.class, IndexCoordinates.of("user_info_add"));

        map.put("user", userSearchHits.getSearchHits());

        map.put("user_info", userInfoSearchHits.getSearchHits());

        map.put("user_info_add", userInfoAddSearchHits.getSearchHits());

        return map;
    }

    @PostMapping("saveUser")
    public Map<String, Object> saveUser(@RequestBody List<User> user) {
        Map<String, Object> map = new HashMap<>(5);
        //简单的保存，索引的indexName为指定的document的indexName
        User user1 = elasticsearchRestTemplate.save(user.get(0));
        map.put("user", user1);


        //指定indexName，如果不存在，创建索引再存入
        User userInfo = elasticsearchRestTemplate.save(user.get(1), IndexCoordinates.of("user_info"));
        map.put("user_info", userInfo);

        User userInfoAdd = elasticsearchRestTemplate.save(user.get(2), IndexCoordinates.of("user_info_add"));
        map.put("user_info_add", userInfoAdd);

        return map;
    }

    @DeleteMapping("deleteUser")
    public Object deleteUser(){

       //根据ID删除IndexName 里面的对应数据
        String delete = elasticsearchRestTemplate.delete("1qaz2wsx3edc", User.class);


//        elasticsearchRestTemplate.delete("1qaz2wsx3edc",IndexCoordinates.of("user_info"));
        return delete;
    }
}
